package com.itcast.flink.order;

import lombok.SneakyThrows;
import org.apache.commons.lang3.time.FastDateFormat;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

import java.time.Duration;
import java.util.Date;

/**
 * 需求：每次统计最近10秒各个用户订单销售额，最大允许乱序时间：2秒，
 * 最大允许延迟时间：3秒，迟到很久数据侧边输出
 *
 * @author lilulu
 */
public class StreamOrderWindowReport {
    public static void main(String[] args) throws Exception {
        // 1. 执行环境-env
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        // 2. 数据源-source
        /*
订单ID, 用户ID, 订单金额, 订单时间

o_101,u_121,11.50,2022-04-05 10:00:02
o_102,u_121,59.50,2022-04-05 10:00:04
o_103,u_121,4.00,2022-04-05 10:00:07
o_104,u_121,22.25,2022-04-05 10:00:10

o_105,u_121,37.35,2022-04-05 10:00:09

o_106,u_121,33.40,2022-04-05 10:00:11
o_107,u_121,4.00,2022-04-05 10:00:12

o_108,u_121,29.10,2022-04-05 10:00:08

o_109,u_121,25.20,2022-04-05 10:00:15
o_110,u_121,58.80,2022-04-05 10:00:06

o_111,u_121,80.90,2022-04-05 10:00:20
o_112,u_121,46.10,2022-04-05 10:00:22
 */
        DataStreamSource<String> inputStream = env.socketTextStream("node1", 9999);
        // 3. 数据转换-transformation
        /**
         *              3-1. 过滤、解析、封装数据到实体类对象
         *             3-2. 指定事件时间字段的值，类型为long
         *             3-3. 对数据流进行分组，设置窗口进行计算
         */
        SingleOutputStreamOperator<OrderEvent> timeWindowStream = inputStream.filter(line -> line.trim().split(",").length == 4)
                .map(new MapFunction<String, OrderEvent>() {
                    @Override
                    public OrderEvent map(String line) throws Exception {
                        System.out.println("order -> " + line);
                        String[] split = line.split(",");
                        OrderEvent orderEvent = new OrderEvent();
                        orderEvent.setOrderId(split[0]);
                        orderEvent.setUserId(split[1]);
                        orderEvent.setOrderMoney(Double.parseDouble(split[2]));
                        orderEvent.setOrderTime(split[3]);
                        return orderEvent;
                    }
                }).assignTimestampsAndWatermarks(WatermarkStrategy.<OrderEvent>forBoundedOutOfOrderness(Duration.ofSeconds(2))
                        .withTimestampAssigner(new SerializableTimestampAssigner<OrderEvent>() {
                            private FastDateFormat format = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");

                            @SneakyThrows
                            @Override
                            public long extractTimestamp(OrderEvent orderEvent, long recordTimestamp) {
                                String orderTime = orderEvent.getOrderTime();
                                Date orderDate = format.parse(orderTime);
                                return orderDate.getTime();
                            }
                        }));

        OutputTag<Tuple2<String, Double>> lateOutPut = new OutputTag<Tuple2<String, Double>>("late-data") {
        };

        SingleOutputStreamOperator<OrderReport> result = timeWindowStream.map(new MapFunction<OrderEvent, Tuple2<String, Double>>() {
            @Override
            public Tuple2<String, Double> map(OrderEvent orderEvent) throws Exception {
                return Tuple2.of(orderEvent.getUserId(), orderEvent.getOrderMoney());
            }
        })
                .keyBy(tuple -> tuple.f0)
                .window(TumblingEventTimeWindows.of(Time.seconds(10)))
                .allowedLateness(Time.seconds(3))
                .sideOutputLateData(lateOutPut)
                .apply(new WindowFunction<Tuple2<String, Double>, OrderReport, String, TimeWindow>() {
                    private FastDateFormat format = FastDateFormat.getInstance("yyyy-Mm-dd HH:mm:ss");

                    @Override
                    public void apply(String key, TimeWindow timeWindow, Iterable<Tuple2<String, Double>> iterable, Collector<OrderReport> collector) throws Exception {
                        String windowEnd = this.format.format(timeWindow.getEnd());
                        String windowStart = this.format.format(timeWindow.getStart());
                        double sum = 0.0;
                        for (Tuple2<String, Double> tuple : iterable) {
                            sum += tuple.f1;
                        }
                        OrderReport orderReport = new OrderReport();
                        orderReport.setUserId(key);
                        orderReport.setWindowStart(windowStart);
                        orderReport.setWindowEnd(windowEnd);
                        orderReport.setTotalMoney(sum);
                        collector.collect(orderReport);
                    }
                });

        // 4. 数据终端-sink
        result.printToErr();

        DataStream<Tuple2<String, Double>> sideOutput = result.getSideOutput(lateOutPut);
        sideOutput.print("late>>");
        // 5. 触发执行-execute
        env.execute("StreamOrderWindowReport");
    }
}